package com.example.testlist.fragment;

import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.MenuItem;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.example.testlist.R;
import com.example.testlist.activity.TestListHomeFragment;
import com.gemall.library.ui.LibraryBaseActivity;

import java.util.ArrayList;

@Route(path = "/testlist/TestListMainActivity")
public class TestListMainActivity extends LibraryBaseActivity {


    private ViewPager mViewPager;
    private BottomNavigationView mBottomNavigationView;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_list_main);

        mViewPager =  findViewById(R.id.pager);
        mBottomNavigationView = findViewById(R.id.bnv_menu);

        final ArrayList<Fragment> list = new ArrayList<>();
        TestListHomeFragment frag01 = new TestListHomeFragment();
        TestListDeviceFragment frag02 = new TestListDeviceFragment();
        TestListMyFragment frag03 = new TestListMyFragment();
        list.add(frag01);
        list.add(frag02);
        list.add(frag03);


        //设置适配器
        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {

                             @Override
                             public int getCount() {
                                 // TODO Auto-generated method stub
                                 return list.size();
                             }

                             @Override
                             public Fragment getItem(int arg0) {
                                 // TODO Auto-generated method stub
                                 return list.get(arg0);
                             }
                         });

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                switch (i){
                    case 0 :
                        mBottomNavigationView.setSelectedItemId(R.id.item_tab0);
                        break;
                    case 1 :
                        mBottomNavigationView.setSelectedItemId(R.id.item_tab1);
                        break;
                    case 2 :
                        mBottomNavigationView.setSelectedItemId(R.id.item_tab2);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });


        mBottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

                int id = menuItem.getItemId();

                if(id == R.id.item_tab0){
                    mViewPager.setCurrentItem(0);
                    return true;

                }else if(id == R.id.item_tab1){
                    mViewPager.setCurrentItem(1);
                    return true;

                }else if(id == R.id.item_tab2){
                    mViewPager.setCurrentItem(2);
                    return true;

                }

                return false;
            }
        });


//        SkuUtils.printf( "6666:"+getPackageName());
//
//        lt.setText("Sending Reply...");
//        lt.setTranslationY(80); // y offset in pixels
//        lt.show();
//
//        new Handler(Looper.myLooper()).postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                lt.success();
//            }
//        },5000);

    }

}
